<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 活动管理
 */

namespace Admin\Controller;
use Model\ActiveModel;

class ActiveController extends BaseController{
    public $aModel;
    public function __construct(){
        parent::__construct();
        $this->aModel = new ActiveModel();
    }

    //活动列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');
        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['title'] = array('like','%'.$sessionArr['name'].'%');
        }
        $resList = $this->aModel->search($where,$page,$this->pernum);
        $totalNum = $this->aModel->getTotalNum($where);
        $pageShow = getPage($totalNum,$this->pernum);
        //所有分类
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('active/index');
    }
    //新增活动
    public function addActive(){
        if(IS_POST){
            $data = $_POST;
            $data['addtime'] = $this->dateNow;
            $id = $this->aModel->addData($data);
            $this->logModel->log('添加活动','active',$id);
            $this->printOut(['code'=>0]);
        }
        //获取分组
        $this->display('active/addactive');
    }
    //编辑活动
    public function editActive(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            unset($_POST['id']);
            $data = $_POST;
            $this->aModel->editData($where,$data);
            $this->logModel->log('编辑活动','active',$where['id']);
            $this->printOut(['code'=>0]);
        }

        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->aModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);
        $this->display('active/editactive');
    }
    //冻结解冻活动
    public function freezeActive(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->aModel->editData($where,$data);
        $this->logModel->log('冻结|解冻活动','active',$_GET['id']);
        redirect($this->domianUrl.'/active/index?p='.$_GET['page'].'&tip=1');
    }
    //删除活动
    public function delActive(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->aModel->editData($where,$data);
        $this->logModel->log('删除活动','active',$_GET['id']);
        redirect($this->domianUrl.'/active/index?p='.$_GET['page'].'&tip=1');
    }

}